﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CUiMFConfigurationPropertiesDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.ModularFixture;

public class CUiMFConfigurationPropertiesDlg : TxForm
{
  private const string USAGE_FILE_NAME = "ListOfValues.xml";
  private const string PLP_TOOL_TYPE_ELEMENT = "toolType";
  private const string META_DATA = "MetaDataExtensions";
  private const string ENUM_FIELD = "EnumerationField";
  private CApMFAdministratorManager m_manager;
  private bool m_viewOnly;
  private IContainer components;
  private Label label1;
  private Label label2;
  private Label label3;
  private Label label4;
  private ComboBox m_configurationType;
  private ComboBox m_usage;
  private TextBox m_name;
  private TextBox m_bitmap;
  private Button m_selectBitmapButton;
  private Button m_ok;
  private Button m_cancel;

  public CUiMFConfigurationPropertiesDlg(CApMFAdministratorManager adminManager, bool viewOnly)
  {
    this.InitializeComponent();
    this.m_manager = adminManager;
    this.m_viewOnly = viewOnly;
  }

  private Array GetPLPToolTypes() => this.GetTypes("toolType");

  private void GetXMLDoc(ref XmlDocument doc)
  {
    string filename = Path.Combine(TxApplicationEx.SysRootAppDataPath(), "ListOfValues.xml");
    doc.Load(filename);
  }

  private Array GetTypes(string category)
  {
    Array types = (Array) null;
    XmlDocument doc = new XmlDocument();
    this.GetXMLDoc(ref doc);
    XmlNodeList elementsByTagName = doc.GetElementsByTagName("MetaDataExtensions");
    if (elementsByTagName != null && elementsByTagName.Count > 0)
    {
      bool flag = true;
      for (int index1 = 0; index1 < elementsByTagName.Count & flag; ++index1)
      {
        XmlNodeList childNodes1 = elementsByTagName.Item(index1).ChildNodes;
        if (childNodes1 != null && childNodes1.Count > 0)
        {
          foreach (XmlNode xmlNode1 in childNodes1)
          {
            if (xmlNode1.Name == "EnumerationField" && xmlNode1.Attributes.Item(0).Value == category)
            {
              XmlNodeList childNodes2 = xmlNode1.ChildNodes;
              if (childNodes2 != null && childNodes2.Count > 0)
              {
                int index2 = 0;
                types = Array.CreateInstance(typeof (string), childNodes2.Count);
                foreach (XmlNode xmlNode2 in childNodes2)
                {
                  if (xmlNode2.Attributes.Item(1).Name == "DisplayedName")
                  {
                    string str = xmlNode2.Attributes.Item(1).Value;
                    types.SetValue((object) str, index2);
                    ++index2;
                  }
                }
              }
              flag = false;
              break;
            }
          }
        }
      }
    }
    return types;
  }

  private void CUiMFConfigurationPropertiesDlg_Load(object sender, EventArgs e)
  {
    try
    {
      Array plpToolTypes = this.GetPLPToolTypes();
      if (plpToolTypes.Length > 0)
        this.InitUsageComboBox(plpToolTypes);
    }
    catch (Exception ex)
    {
      int num = (int) MessageBox.Show(ex.Message);
      return;
    }
    if (this.m_viewOnly)
    {
      this.m_name.Text = this.m_manager.ConfigurationData.Name;
      if (this.m_manager.ConfigurationData is CApMFConfigurationTypeData)
      {
        CApMFConfigurationTypeData configurationData = this.m_manager.ConfigurationData as CApMFConfigurationTypeData;
        this.m_usage.SelectedValue = (object) configurationData.PlpUsage;
        this.m_usage.SelectedItem = (object) configurationData.PlpUsage;
        this.m_configurationType.SelectedIndex = 0;
      }
      else if (this.m_manager.ConfigurationData is CApMFUnitTypeData)
        this.m_configurationType.SelectedIndex = 1;
      else
        this.m_configurationType.SelectedIndex = 2;
      this.m_name.ReadOnly = true;
      this.m_configurationType.Enabled = false;
      this.m_usage.Enabled = false;
      this.m_bitmap.Text = this.m_manager.ConfigurationData.BitmapFile;
    }
    else
    {
      this.m_usage.SelectedIndex = 0;
      this.m_configurationType.SelectedIndex = 0;
      this.m_ok.Enabled = false;
    }
  }

  private void InitUsageComboBox(Array plpTypes)
  {
    this.m_usage.Items.Clear();
    foreach (object plpType in plpTypes)
      this.m_usage.Items.Add(plpType);
  }

  private void m_ok_Click(object sender, EventArgs e)
  {
    if (!this.m_viewOnly)
    {
      this.m_manager.SetConfigurationData(this.m_name.Text, this.m_configurationType.SelectedIndex, this.m_usage.Text, this.m_bitmap.Text);
      this.m_manager.CreateRootNode(this.m_name.Text);
    }
    else
      this.m_manager.UpdateConfigurationData(this.m_bitmap.Text);
    ((Form) this).Close();
  }

  private void m_cancel_Click(object sender, EventArgs e) => ((Form) this).Close();

  private void SelectBitmapButton_Click(object sender, EventArgs e)
  {
    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
      openFileDialog.CheckFileExists = true;
      openFileDialog.Multiselect = false;
      openFileDialog.Filter = "Image Files (*.gif;*.jpg;*.bmp)|*.gif;*.jpg;*.bmp";
      openFileDialog.RestoreDirectory = true;
      openFileDialog.Title = "Select Bitmap File";
      if (openFileDialog.ShowDialog() != DialogResult.OK)
        return;
      this.m_bitmap.Text = openFileDialog.FileNames[0];
    }
  }

  private void m_name_TextChanged(object sender, EventArgs e)
  {
    if (this.m_name.Text == "")
      this.m_ok.Enabled = false;
    else
      this.m_ok.Enabled = true;
  }

  private void m_configurationType_SelectedIndexChanged(object sender, EventArgs e)
  {
    if (this.m_configurationType.SelectedIndex == 0)
      this.m_usage.Enabled = true;
    else
      this.m_usage.Enabled = false;
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiMFConfigurationPropertiesDlg));
    this.label1 = new Label();
    this.label2 = new Label();
    this.label3 = new Label();
    this.label4 = new Label();
    this.m_configurationType = new ComboBox();
    this.m_usage = new ComboBox();
    this.m_name = new TextBox();
    this.m_bitmap = new TextBox();
    this.m_selectBitmapButton = new Button();
    this.m_ok = new Button();
    this.m_cancel = new Button();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.label1, "label1");
    this.label1.Name = "label1";
    componentResourceManager.ApplyResources((object) this.label2, "label2");
    this.label2.Name = "label2";
    componentResourceManager.ApplyResources((object) this.label3, "label3");
    this.label3.Name = "label3";
    componentResourceManager.ApplyResources((object) this.label4, "label4");
    this.label4.Name = "label4";
    this.m_configurationType.DropDownStyle = ComboBoxStyle.DropDownList;
    this.m_configurationType.FormattingEnabled = true;
    this.m_configurationType.Items.AddRange(new object[3]
    {
      (object) componentResourceManager.GetString("m_configurationType.Items"),
      (object) componentResourceManager.GetString("m_configurationType.Items1"),
      (object) componentResourceManager.GetString("m_configurationType.Items2")
    });
    componentResourceManager.ApplyResources((object) this.m_configurationType, "m_configurationType");
    this.m_configurationType.Name = "m_configurationType";
    this.m_configurationType.SelectedIndexChanged += new EventHandler(this.m_configurationType_SelectedIndexChanged);
    this.m_usage.DropDownStyle = ComboBoxStyle.DropDownList;
    this.m_usage.FormattingEnabled = true;
    componentResourceManager.ApplyResources((object) this.m_usage, "m_usage");
    this.m_usage.Name = "m_usage";
    componentResourceManager.ApplyResources((object) this.m_name, "m_name");
    this.m_name.Name = "m_name";
    this.m_name.TextChanged += new EventHandler(this.m_name_TextChanged);
    componentResourceManager.ApplyResources((object) this.m_bitmap, "m_bitmap");
    this.m_bitmap.Name = "m_bitmap";
    this.m_bitmap.ReadOnly = true;
    componentResourceManager.ApplyResources((object) this.m_selectBitmapButton, "m_selectBitmapButton");
    this.m_selectBitmapButton.Name = "m_selectBitmapButton";
    this.m_selectBitmapButton.UseVisualStyleBackColor = true;
    this.m_selectBitmapButton.Click += new EventHandler(this.SelectBitmapButton_Click);
    this.m_ok.DialogResult = DialogResult.OK;
    componentResourceManager.ApplyResources((object) this.m_ok, "m_ok");
    this.m_ok.Name = "m_ok";
    this.m_ok.UseVisualStyleBackColor = true;
    this.m_ok.Click += new EventHandler(this.m_ok_Click);
    this.m_cancel.DialogResult = DialogResult.Cancel;
    componentResourceManager.ApplyResources((object) this.m_cancel, "m_cancel");
    this.m_cancel.Name = "m_cancel";
    this.m_cancel.UseVisualStyleBackColor = true;
    this.m_cancel.Click += new EventHandler(this.m_cancel_Click);
    ((Form) this).AcceptButton = (IButtonControl) this.m_ok;
    componentResourceManager.ApplyResources((object) this, "$this");
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((Form) this).CancelButton = (IButtonControl) this.m_cancel;
    ((Control) this).Controls.Add((Control) this.m_cancel);
    ((Control) this).Controls.Add((Control) this.m_ok);
    ((Control) this).Controls.Add((Control) this.m_selectBitmapButton);
    ((Control) this).Controls.Add((Control) this.m_bitmap);
    ((Control) this).Controls.Add((Control) this.m_name);
    ((Control) this).Controls.Add((Control) this.m_usage);
    ((Control) this).Controls.Add((Control) this.m_configurationType);
    ((Control) this).Controls.Add((Control) this.label4);
    ((Control) this).Controls.Add((Control) this.label3);
    ((Control) this).Controls.Add((Control) this.label2);
    ((Control) this).Controls.Add((Control) this.label1);
    ((Form) this).FormBorderStyle = FormBorderStyle.FixedDialog;
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CUiMFConfigurationPropertiesDlg);
    ((Form) this).ShowIcon = false;
    ((Form) this).Load += new EventHandler(this.CUiMFConfigurationPropertiesDlg_Load);
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }
}
